<template>
  <div class="informationadd">
    <div class="outmain ba_f heighthv pad_20 bor_rad5">
      <div class="shuju_title mar_b20">
        <div class="shuju_title_text">
          <span>资讯设置</span>
        </div>
      </div>
      <div class="mar_t20 flex-bet flex-y-top">
        <el-form ref="form" :model="form" label-width="180px">
          <el-form-item label="资讯开关" :required="true">
            <el-tooltip :content="form.display==1?'开启':'关闭'" placement="top">
              <el-switch v-model="form.display" active-value="1" inactive-value="2"></el-switch>
            </el-tooltip>
            <div class="yb_margin lh16">如关闭此功能商家将不显示资讯内容</div>
          </el-form-item>
          <el-form-item label="资讯审核" :required="true">
            <el-tooltip :content="form.display==1?'开启':'关闭'" placement="top">
              <el-switch v-model="form.display" active-value="1" inactive-value="2"></el-switch>
            </el-tooltip>
          </el-form-item>
          <!--                        <el-form-item label="封面图片">-->
          <!--                            <div class="uploadSource bor_de rel flex" v-if="form.icon">-->
          <!--                                <label class="statusLabel curs" @mouseenter="enter(1)" @mouseleave="leave(1)">-->
          <!--                                    <i class="el-icon-check" ref="imgicon1"></i>-->
          <!--                                    <i class="el-icon-close" @click="deleteMultiImage(form.icon,1)"></i>-->
          <!--                                </label>-->
          <!--                                <img :src="form.icon" @click='test(1)' class="wid"/>-->
          <!--                            </div>-->
          <!--                            <div class="uploadSource flex-center curs" v-else @click='test(1)'>-->
          <!--                                <i class="el-icon-plus fon_40 color_d9"></i>-->
          <!--                            </div>-->
          <!--                            <div class="yb_margin">建议尺寸：720*180</div>-->
          <!--                        </el-form-item>-->
          <!--                        <el-form-item label="资讯详情">-->
          <!--                            <baidu-ueditor :content='form.details' @ueditor='getUeditor'></baidu-ueditor>-->
          <!--                        </el-form-item>-->
        </el-form>
      </div>
    </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
    <attachment-img ref="img1" type="img" @attachment="imgadd1"></attachment-img>
  </div>
</template>
<script>
import { saveNewReduction, getsaveNewReduction } from '@/api/plug';
import { changeDate } from '@/utils/index';
import attachmentImg from '@/plugins/attachment-img';
import baiduUeditor from '@/plugins/baidu-ueditor';

export default {
  created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.edit();
    }
  },
  components: { attachmentImg, baiduUeditor },
  data() {
    return {
      form: {
        name: '',
        money: '',
        details: '',
        useType: '1'
      },
      money1: '',
      money2: '',
      money3: '',
      money4: '',
      numType: '1',
      timeData: [new Date(), new Date().setMonth(new Date().getMonth() + 3)],
      collapseName: ''
    };
  },
  methods: {
    getUeditor(content) {
      this.form.details = content;
    },
    async edit() {
      const { data } = await getsaveNewReduction({ id: this.id });
      this.form2 = this.form;
      this.form = Object.assign(this.form2, data);
      if (data.startTime) {
        this.timeData = [data.startTime, data.endTime];
      }
    },
    enter(i = 0) {
      this.$refs['imgicon' + i].style.display = 'none';
    },
    leave(i = 0) {
      this.$refs['imgicon' + i].style.display = 'inline-block';
    },
    test(i = 0) {
      console.log(this.$refs, i);
      this.$refs['img' + i].dialogVisible = true;
    },
    imgadd1(url) {
      this.form.icon = url.str;
    },
    deleteMultiImage(item, index, type) {
      //typem没传是单图multigraph多图；index上传图片组件排序
      Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
          this.splice(index, 1);
        }
      };
      if (type == 'multigraph') {
        switch (index) {
          case 2:
            this.form2.environment.remove(item);
            break;
        }
      } else {
        switch (index) {
          case 1:
            this.form.icon = '';
            break;
        }
      }
    },
    async submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.timeData) {
            this.form.startTime = changeDate(this.timeData[0]);
            this.form.endTime = changeDate(this.timeData[1]);
          }
          const { msg } = await saveNewReduction(this.form);
          this.$baseMessage(msg, 'success');
          this.$router.go(-1);
        } else {
          return false;
        }
      });
    }
  }
};
</script>
